package com.ruoyi.applet.appletReceivingAddress.service.impl;

import com.github.pagehelper.Page;
import com.ruoyi.applet.appletReceivingAddress.domain.AppletReceivingAddress;
import com.ruoyi.applet.appletReceivingAddress.mapper.AppletReceivingAddressMapper;
import com.ruoyi.applet.appletReceivingAddress.service.IAppletReceivingAddressService;
import com.ruoyi.applet.entity.PageR;
import com.ruoyi.applet.entity.ReceivingAddressApplet;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序用户收货地址Service业务层处理
 * @author ruoyi
 * @date 2024-12-17
 */
@Service
public class AppletReceivingAddressServiceImpl implements IAppletReceivingAddressService {
    
    @Resource
    private AppletReceivingAddressMapper appletReceivingAddressMapper;
    
    /**
     * 查询小程序用户收货地址
     * @param id 小程序用户收货地址主键
     * @return 小程序用户收货地址
     */
    @Override
    public AppletReceivingAddress selectAppletReceivingAddressById(Long id) {
        return appletReceivingAddressMapper.selectAppletReceivingAddressById(id);
    }
    
    /**
     * 查询小程序用户收货地址列表
     * @param appletReceivingAddress 小程序用户收货地址
     * @return 小程序用户收货地址
     */
    @Override
    public List<AppletReceivingAddress> selectAppletReceivingAddressList(AppletReceivingAddress appletReceivingAddress) {
        return appletReceivingAddressMapper.selectAppletReceivingAddressList(appletReceivingAddress);
    }
    
    /**
     * 新增小程序用户收货地址
     * @param appletReceivingAddress 小程序用户收货地址
     * @return 结果
     */
    @Override
    public int insertAppletReceivingAddress(AppletReceivingAddress appletReceivingAddress) {
        appletReceivingAddress.setCreateBy(String.valueOf(appletReceivingAddress.getWxUserId()));
        appletReceivingAddress.setCreateTime(DateUtils.getNowDate());
        appletReceivingAddress.setUpdateTime(DateUtils.getNowDate());
        
        if (appletReceivingAddress.getIsDefault() == 1) {
            appletReceivingAddressMapper.updateDefaultByWxUserId(appletReceivingAddress.getWxUserId());
        }
        return appletReceivingAddressMapper.insertAppletReceivingAddress(appletReceivingAddress);
    }
    
    /**
     * 修改小程序用户收货地址
     * @param appletReceivingAddress 小程序用户收货地址
     * @return 结果
     */
    @Override
    public int updateAppletReceivingAddress(AppletReceivingAddress appletReceivingAddress) {
        appletReceivingAddress.setUpdateBy(String.valueOf(appletReceivingAddress.getWxUserId()));
        appletReceivingAddress.setUpdateTime(DateUtils.getNowDate());
        
        if (appletReceivingAddress.getIsDefault() == 1) {
            appletReceivingAddressMapper.updateDefaultByWxUserId(appletReceivingAddress.getWxUserId());
        }
        return appletReceivingAddressMapper.updateAppletReceivingAddress(appletReceivingAddress);
    }
    
    /**
     * 批量删除小程序用户收货地址
     * @param ids 需要删除的小程序用户收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAppletReceivingAddressByIds(String ids) {
        return appletReceivingAddressMapper.deleteAppletReceivingAddressByIds(Convert.toStrArray(ids));
    }
    
    /**
     * 删除小程序用户收货地址信息
     * @param id 小程序用户收货地址主键
     * @return 结果
     */
    @Override
    public int deleteAppletReceivingAddressById(Long id) {
        return appletReceivingAddressMapper.deleteAppletReceivingAddressById(id);
    }
    
    /**
     * 查询小程序用户收货地址列表
     * @param receivingAddressApplet 查询条件
     * @return {@link Page <AppletReceivingAddress>}
     */
    @Override
    public PageR<AppletReceivingAddress> selectAppletReceivingAddressListApplet(ReceivingAddressApplet receivingAddressApplet) {
        int count = appletReceivingAddressMapper.selectAppletReceivingAddressCount(receivingAddressApplet);
        PageR<AppletReceivingAddress> pageR = new PageR<>();
        pageR.setPageNo(receivingAddressApplet.getPageNo());
        pageR.setPageSize(receivingAddressApplet.getPageSize());
        pageR.setTotal(count);
        if (count == 0) {
            return pageR;
        }
        int start = (receivingAddressApplet.getPageNo() - 1) * receivingAddressApplet.getPageSize();
        List<AppletReceivingAddress> shoppingCartAppletList = appletReceivingAddressMapper.selectAppletReceivingAddressListApplet(receivingAddressApplet, receivingAddressApplet.getPageSize(), start);
        
        pageR.setReCords(shoppingCartAppletList);
        return pageR;
    }
}
